﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace ctem.gui.SignalChart
{
    public class Chart
    {
        
        
        private RectangleF m_ViewRect;

        //private Point m_BegOfCoords;
        //internal Axis X_Axis;
        //internal Axis Y_Axis;
        
        private List<Series> m_SeriesList = new List<Series>();

        public int CountOfSeries { get { return m_SeriesList.Count; } }
        
        public int CountOfPartsX = 8;
        public int CountOfPartsY = 8;

        public RectangleF ViewRect { get { return m_ViewRect; } set { m_ViewRect = value; } }

        public Chart()
        {
            //X_Axis = new Axis(m_BegOfCoords, new Point(10000, m_BegOfCoords.Y), TAxisDirection.LeftRight);
            //Y_Axis = new Axis(m_BegOfCoords, new Point(m_BegOfCoords.X, -10000), TAxisDirection.DownUp);
        }

        public void Clear()
        {
            for (int i = 0; i < CountOfSeries; i++)
                m_SeriesList[i].Clear();
            //time = 0;
        }
        public void RemoveAllSeries()
        {
            m_SeriesList.RemoveRange(0,CountOfSeries);
        }

        public void ClearChart()
        {
            for (int i = 0; i < m_SeriesList.Count; i++)
                m_SeriesList[i].Clear();
        }

        public void AddSeries(Series s)
        {
            s.owner = this;
            m_SeriesList.Add(s);
        }

        public Series GetSeries(string name)
        {
            foreach (Series s in m_SeriesList)
                if (s.Name == name) return s;
            return null;
        }

        public Series GetSeries(int index)
        {
            if ((index >= 0) && (index < m_SeriesList.Count))
                return m_SeriesList[index];
            else
                return null;
        }

        internal void RedrawChart(Graphics g,int canvas_w, int canvas_h, bool IsLog)
        {
            g.Clear(Color.White);

            //float delta = (float)(ViewRect.Width / X_Axis.CountOfParts);
            float delta_Re;
            // решёточка
            if (IsLog)
            {
                int count_of_parts = (int)Math.Truncate(Math.Log10(ViewRect.Width));

                for (int i = 0; i < count_of_parts + 1; i++)
                {
                    delta_Re = (float)(canvas_w / (Math.Log10(ViewRect.Right) - Math.Log10(ViewRect.Left)));
                    //float part = (float)ViewRect.Width / 3;
                    float k = (float)(delta_Re) / (float)(Math.Log10(delta_Re));
                    float delta1 = (float)(delta_Re) / 9F;
                    for (int j = 1; j < 10; j++)
                        g.DrawLine(new Pen(Brushes.Beige, 1), new PointF((float)(i * delta_Re + k * Math.Log10(j * delta1)), 0), new PointF((float)(i * delta_Re + k * Math.Log10(j * delta1)), canvas_h));
                }
            }
            else
            {
                delta_Re = (float)(canvas_w) / (float)(CountOfPartsX);

                for (int i = 1; i < CountOfPartsX; i++)
                {
                    g.DrawLine(new Pen(Brushes.Beige, 1), new PointF(i * delta_Re, 0), new PointF(i * delta_Re, canvas_h));
                }

            }
            delta_Re = (float)(canvas_h) / (float)(CountOfPartsY);

            for (int i = 0; i < CountOfPartsY; i++)
                g.DrawLine(new Pen(Brushes.Beige, 1), new PointF(0, i * delta_Re), new PointF(canvas_w, i * delta_Re));

            //линия нуля
            g.DrawLine(new Pen(Brushes.Purple, 1), new PointF(0, canvas_h + ViewRect.Top * canvas_h / ViewRect.Height), new PointF(canvas_w, canvas_h + ViewRect.Top * canvas_h / ViewRect.Height));


            foreach (Series s in m_SeriesList)
                s.Draw(ViewRect,canvas_w,canvas_h, g, IsLog);
        }


    }
}
